package easy

import (
	"workspace/node"
)

func SumOfLeftLeaves(root *node.TreeNode) int {
	if root == nil {
		return 0
	}
	return dfs(root)
}

func dfs(root *node.TreeNode) int {
	ans := 0
	if root.Left != nil {
		if isLeafNode(root.Left) {
			ans += root.Left.Val
		} else {
			ans += dfs(root.Left)
		}
	}
	if root.Right != nil && !isLeafNode(root.Right) {
		ans += dfs(root.Right)
	}
	return ans
}

func isLeafNode(node *node.TreeNode) bool {
	return node.Left == nil && node.Right == nil
}
